package com.junxian.demo.listen;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.junxian.demo.model.OsdExample;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sunjx
 * @program giteeCode
 * @description 监听器
 * @date 2020-08-31 19:07
 */
public class ExcelListener extends AnalysisEventListener<OsdExample> {

    private static final int BATCH_COUNT = 5;
    List<OsdExample> list = new ArrayList<OsdExample>();
    private static int count = 1;

    @Override
    public void invoke(OsdExample osdExample, AnalysisContext analysisContext) {
        System.out.println("解析到一条数据:{ "+ osdExample.toString() +" }");
        list.add(osdExample);
        count ++;
        if (list.size() >= BATCH_COUNT) {
            saveData( count );
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData(count);
        System.out.println("所有数据解析完成！");
        System.out.println(" count ：" + count);
    }

    /**
     * 加上存储数据库
     */
    private void saveData(int count) {
        System.out.println("{ "+ count +" }条数据，开始存储数据库！" + list.size());
        System.out.println("存储数据库成功！");
    }
}
